home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / getcmt.com / GETCMT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-22  |  6.7 KB  |  170 lines

  1. /* getcmt.c - get comments from a C or C++ source file    */
  2. /*
  3.                                Byte_Magic Software
  4.                              9850 Meadowglen Ln. #35
  5.                                Houston, Tx.  77042
  6.                                  (713) 975-9033
  7.  
  8. Author:     Greg Messer
  9. Program:    getcmt.c
  10. Purpose:    Isolate comments from a C or C++ source file. Filter.
  11. Syntax:     getcmt    (use redirection for file and/or device I/O)
  12. Release:    Released to the Public Domain by Byte_Magic Software.
  13. Date:       07-11-88 GM...
  14.                 First release.
  15. Revised:    01-13-90 GM...
  16.                 Streamlined logic.
  17.             01-15-90 Bob Stout...
  18.                 Fixed unsigned char error as return from getc().
  19.             01-22-90 GM...
  20.                 Fixed bug handling "xx/" (xx = **) at end of comment.
  21.                 Added C++ comment extraction.
  22.                 Added return of count to OS (ERRORLEVEL in MS-DOS).
  23. System:     Compiled with Microsoft QuickC/Assembler V2.01 under MS-DOS 3.10
  24.             for IBM PCs and compatibles.
  25. Rules:      ANSI C comments begin with /x and end with x/. (x = *).
  26.             Comments do not nest and do not appear in string or character
  27.             constants.
  28.             C++ comments begin with double slashes and end at EOL.
  29.             A Microsoft extension to C allows C++ style comments to serve as
  30.             single-line comments in C source.
  31. Comments:   Useful for creating documentation and improving commenting style.
  32.             Input and output are from stdin and stdout respectively, so use DOS
  33.             redirection for input and output.  Messages go to stderr so they
  34.             are not redirectable away from the screen.
  35.             Returns ERRORLEVEL = number of comments in source file(s).
  36. Examples:
  37.             Example... Output to screen:
  38.             getcmt < cfile.c
  39.                (displays comments from cfile.c on screen)
  40.             type cfile.c | getcmt
  41.                (same as above but slightly slower)
  42.             getcmt < cfile.c | more
  43.                (same as above, but pauses after each full screen)
  44.  
  45.             Example... Output to printer:
  46.             getcmt < cfile.c > prn
  47.                (same as above but prints output on printer)
  48.             type cfile.c | getcmt > prn
  49.                (same as above but slightly slower)
  50.  
  51.             Example... Output to file:
  52.             getcmt < cfile.c > cfile.cmt
  53.                (writes cfile.c comments to cfile.cmt, overwriting existing file)
  54.             getcmt < cfile.c >> cfile.doc
  55.                (writes cfile.c comments to end of cfile.doc (appends))
  56.  
  57.             For complete instructions on using redirection symbols, consult
  58.             the PC-DOS or MS-DOS manual or a general DOS reference book.
  59. */
  60.  
  61. #include <stdlib.h>
  62. #include <stdio.h>
  63.  
  64. #define LOOKS_GREAT 0
  65. #define LESS_FILLING 1
  66.  
  67. int extract_c_cmts(void);
  68. void inside_c_cmt(int);
  69.  
  70. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  71.  
  72. main()                                      /* main logic:                  */
  73. {
  74.     register int i;
  75.     char *hype = {
  76.         "\nGETCMT v1.1 - GET CoMmenTs\nby Byte_Magic Software"
  77.         };
  78.     char *help = {
  79.         "\nUsage:  GETCMT < sourcefile.ext [> destination file or device]\n"
  80.         "\nReading source code from stdin (Ctrl-C to quit before EOF) ...\n"
  81.         };
  82.                                             /* display messages to operator */
  83. #if LOOKS_GREAT
  84.     fputs(hype, stderr);
  85.     fputs(help, stderr);
  86. #elif LESS_FILLING
  87.     i = 0;
  88.     while(hype[i] != '\0')
  89.         putc(hype[i++], stderr);
  90.     i = 0;
  91.     while(help[i] != '\0')
  92.         putc(help[i++], stderr);
  93. #endif
  94.  
  95.     i = extract_c_cmts();                   /* extract comments in stdin    */
  96.     putc('\n', stdout);
  97.  
  98.     return(i);                              /* return number of comments to */
  99.                                             /* OS (ERRORLEVEL in DOS)        */
  100. }
  101.  
  102. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  103.  
  104. int extract_c_cmts()                        /* comment extraction logic:    */
  105. {
  106.     register int chi, cht;              /* chi = char in, cht = char test   */
  107.     int count;                          /* count = comment count            */
  108.  
  109.     count = 0;
  110.     chi = getc(stdin);
  111.     while(chi != EOF)                       /* as long as there is input... */
  112.     {
  113.         if(chi == '/')                      /* process comments             */
  114.         {
  115.             cht = getc(stdin);
  116.             if(cht == EOF)
  117.                 return(count);
  118.             if(cht == '*' || cht == '/')    /* if start of a comment...     */
  119.             {
  120.                 count++;                    /* count it and                 */
  121.                 inside_c_cmt(cht);          /* output all of the comment    */
  122.             }
  123.             else
  124.                 ungetc(cht, stdin);
  125.         }
  126.         chi = getc(stdin);                  /* continue scanning input      */
  127.     }
  128.     return(count);
  129. }
  130.  
  131. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  132.  
  133. void inside_c_cmt(int ch)                   /* comment output logic:        */
  134. {                                           /* input ch = either '*' for C  */
  135.                                             /* or '/' for C++               */
  136.  
  137.     register int chi, cht;              /* chi = char in, cht = char test   */
  138.  
  139.     if(ch == '/')                           /* make ch = '\n' if C++        */
  140.         ch = '\n';                          /* note: ch is already 1st char */
  141.                                             /* of end comment if this is C  */
  142.     putc('\n', stdout);
  143.     chi = getc(stdin);
  144.     while(chi != EOF)                       /* as long as there is input... */
  145.     {                                       /* process comments             */
  146.         if(chi == ch)
  147.         {
  148.             if(ch == '\n')                  /* if C++ comment is ended...   */
  149.                 return;                     /* stop outputting              */
  150.             cht = getc(stdin);
  151.             if(cht == '/')                  /* if C comment is ended...     */
  152.                 return;                     /* stop outputting              */
  153.             else
  154.             {
  155.                 ungetc(cht, stdin);
  156.                 putc(chi, stdout);
  157.             }
  158.         }
  159.         else
  160.             putc(chi, stdout);              /* else comment text, output it */
  161.                                             
  162.         chi = getc(stdin);                  /* continue scanning input      */
  163.     }
  164.     return;
  165. }
  166.  
  167. /*  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   */
  168. /* end of getcmt.c  */
  169.  
  170.